<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="150px" :rules="formRules">
                <el-form-item label="接口名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入接口名称" />
                </el-form-item>
                <el-form-item label="接口类型" prop="cid">
                    <el-select class="w-80" v-model="formData.cid" placeholder="请选择接口类型" clearable>
                        <el-option v-for="item in optionsData.interface_cate" :key="item.id" :label="item.name"
                            :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="平台消耗点券数" prop="agents_cost">
                    <div>
                        <el-input-number v-model="formData.cost" :min="0" :max="9999" />
                        <div class="form-tips"></div>
                    </div>
                </el-form-item>
                <el-form-item label="代理商消耗点券数" prop="agents_cost">
                    <div>
                        <el-input-number v-model="formData.agents_cost" :min="0" :max="9999" />
                        <div class="form-tips"></div>
                    </div>
                </el-form-item>
                <el-form-item label="渠道号消耗点券数" prop="channel_cost">
                    <div>
                        <el-input-number v-model="formData.channel_cost" :min="0" :max="9999" />
                        <div class="form-tips"></div>
                    </div>
                </el-form-item>
                <el-form-item label="接口标识" prop="uri">
                    <el-input v-model="formData.uri" clearable placeholder="请输入接口标识" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        <div class="form-tips">默认为0， 数值越大越排前</div>
                    </div>
                </el-form-item>
                <el-form-item label="状态" prop="is_show">
                    <el-switch v-model="formData.is_show" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="interfaceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiInterfaceAdd, apiInterfaceEdit, apiInterfaceDetail } from '@/api/interface'
import { apiInterfaceCateAll } from '@/api/interface_cate'
import { useDictOptions } from '@/hooks/useDictOptions'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑接口表' : '新增接口表'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    cid: '',
    cost: '',
    agents_cost: '',
    channel_cost: '',
    sort: '',
    is_show: 1,
    uri: ''
})

// 表单验证
const formRules = reactive<any>({
    name: [
        {
            required: true,
            message: '请输入接口名称',
            trigger: ['blur']
        }
    ],
    cid: [
        {
            required: true,
            message: '请选择接口类型',
            trigger: ['blur']
        }
    ],
    cost: [
        {
            required: true,
            message: '请输入点券数',
            trigger: ['blur']
        }
    ],
    agents_cost: [
        {
            required: true,
            message: '请输入点券数',
            trigger: ['blur']
        }
    ],
    channel_cost: [
        {
            required: true,
            message: '请输入点券数',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入排序',
            trigger: ['blur']
        }
    ],
    uri: [
        {
            required: true,
            message: '请输入接口标识',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiInterfaceDetail({
        id: row.id
    })
    setFormData(data)
}

const { optionsData } = useDictOptions<{
    interface_cate: any[]
}>({
    interface_cate: {
        api: apiInterfaceCateAll
    }
})

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiInterfaceEdit(data) : await apiInterfaceAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
